package controller.welcome;

import controller.common.BaseController;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.log.LoginLogServices;
import services.upms.PermissionServices;
import util.cryptogram.MD5Util;
import wt.entity.log.LoginLog;
import wt.entity.upms.Account;
import wt.staticData.ShortData;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("welcome/signIn")
public class SignInController extends BaseController {
    @Autowired
    PermissionServices permissionServices;
    @Autowired
    LoginLogServices loginLogServices;

    @ResponseBody
    @RequestMapping("out")
    public CommonResult out(String loginName, String password, HttpServletRequest request) throws Exception {

        CommonResult commonResult = validator(loginName, password, request);

        if (commonResult.getStatus() == 1) {
            Account loginAccount = getLoginAccount();
            if (loginAccount.getStaffId() != null) {
                SecurityUtils.getSubject().logout();
                loginLogServices.insertSelective(new LoginLog(loginName, request.getRemoteAddr(), new Date(), ShortData.s0, ShortData.s0));
                commonResult.setMessage("登录失败；登录被拒绝！");
                commonResult.setError();
            } else {
                loginLogServices.insertSelective(new LoginLog(loginName, request.getRemoteAddr(), new Date(), ShortData.s1, null));
                commonResult.setSuccess();
            }
        }
        return commonResult;


    }

    @ResponseBody
    @RequestMapping("in")
    public CommonResult in(String loginName, String password, HttpServletRequest request) throws Exception {

        CommonResult commonResult = validator(loginName, password, request);
        if (commonResult.getStatus() == 1) {
            Account loginAccount = getLoginAccount();
            if (loginAccount.getExpertId() != null) {
                SecurityUtils.getSubject().logout();
                loginLogServices.insertSelective(new LoginLog(loginName, request.getRemoteAddr(), new Date(), ShortData.s0, ShortData.s0));
                commonResult.setMessage("登录失败；登录被拒绝！");
                commonResult.setError();
            } else {
                loginLogServices.insertSelective(new LoginLog(loginName, request.getRemoteAddr(), new Date(), ShortData.s1, null));
                commonResult.setSuccess();
            }
        }
        return commonResult;


    }

    @ResponseBody
    @RequestMapping("all")
    public CommonResult all(String loginName, String password, HttpServletRequest request) throws Exception {
        CommonResult commonResult = validator(loginName, password, request);
        if (commonResult.getStatus() == 1) {
            loginLogServices.insertSelective(new LoginLog(loginName, request.getRemoteAddr(), new Date(), ShortData.s1, null));
            commonResult.setSuccess();
        }
        return commonResult;
    }

    private CommonResult validator(String loginName, String password, HttpServletRequest request) throws Exception {
        CommonResult commonResult = new CommonResult();
        Subject subject = SecurityUtils.getSubject();
        password = MD5Util.md5Hex(password);
        UsernamePasswordToken token = new UsernamePasswordToken(loginName, password);
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            loginLogServices.insertSelective(new LoginLog(loginName, request.getRemoteAddr(), new Date(), ShortData.s0, ShortData.s1));
            commonResult.setMessage("登录失败；用户名或密码错误！");
            return commonResult.setError();
        } catch (IncorrectCredentialsException e) {
            loginLogServices.insertSelective(new LoginLog(loginName, request.getRemoteAddr(), new Date(), ShortData.s0, ShortData.s2));
            commonResult.setMessage("登录失败；用户名或密码错误！");
            return commonResult.setError();
        } catch (LockedAccountException e) {
            loginLogServices.insertSelective(new LoginLog(loginName, request.getRemoteAddr(), new Date(), ShortData.s0, ShortData.s3));
            commonResult.setMessage("登录失败；账户被锁定！");
            return commonResult.setError();
        } catch (AuthenticationException e) {
            loginLogServices.insertSelective(new LoginLog(loginName, request.getRemoteAddr(), new Date(), ShortData.s0, ShortData.s0));
            commonResult.setMessage("登录失败；验证异常！");
            return commonResult.setError();
        }
        return commonResult.setSuccess();
    }


}